# -*- coding: iso-8859-1 -*-
# file HingeJoint.py
# author Rocco Martino
#
############################################################################
#    Copyright (C) 2015 by Rocco Martino                                   #
#    martinorocco@gmail.com                                                #
#                                                                          #
#    This program is free software; you can redistribute it and/or modify  #
#    it under the terms of the GNU Lesser General Public License as        #
#    published by the Free Software Foundation; either version 2.1 of the  #
#    License, or (at your option) any later version.                       #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU Lesser General Public License for more details.                   #
#                                                                          #
#    You should have received a copy of the GNU Lesser General Public      #
#    License along with this program; if not, write to the                 #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

############################################################################
from . import Joint

from mathutils import Vector, Matrix, Euler
############################################################################




############################################################################
# ........................................................................ #
# ........................................................................ #
############################################################################




############################################################################
# ........................................................................ #
class HingeJoint(Joint.Joint):





############################################################################
############################################################################









############################################################################
    def __init__(self, obj, data, constraint):
        super(HingeJoint, self).__init__(obj, data, constraint)

        self.ClassName = "osgODE::HingeJoint"
############################################################################




############################################################################
    def writeData(self, writer):
        super(HingeJoint, self).writeData(writer)





        if self.BlenderObject.use_angular_limit_x:

            writer.writeLine("dParamLoStop1 %f" %self.BlenderObject.limit_angle_min_x)
            writer.writeLine("dParamHiStop1 %f" %self.BlenderObject.limit_angle_max_x)



        erp = self.Data.ERP * 60.0 / self.Data.CurrentScene.game_settings.fps
        cfm = self.Data.CFM * 60.0 / self.Data.CurrentScene.game_settings.fps
        stop_erp = self.Data.StopERP * 60.0 / self.Data.CurrentScene.game_settings.fps
        stop_cfm = self.Data.StopCFM * 60.0 / self.Data.CurrentScene.game_settings.fps




        writer.writeLine("dParamCFM1 %f" %(cfm))

        writer.writeLine("dParamStopERP1 %f" %(stop_erp)) ;
        writer.writeLine("dParamStopCFM1 %f" %(stop_cfm)) ;

        writer.writeLine("dParamERP1 %f" %(erp))






        o1 = self.Body1
        o2 = self.BlenderObject.target

        mat1 = Matrix(o1.matrix_world)

        if o2:
            mat1.invert()
            mrel = mat1 * Matrix(o2.matrix_world)
            relative_position, relative_rotation, srel = mrel.decompose()
        else:
            relative_position, relative_rotation, srel = mat1.decompose()
            mat1.invert()
            tmp, relative_rotation, srel = mat1.decompose()



        writer.writeLine("AutoComputeRelativeValues FALSE")

        writer.writeLine("RelativeRotation %f %f %f %f" %(relative_rotation[1], relative_rotation[2], relative_rotation[3], relative_rotation[0]))
############################################################################




# ........................................................................ #
############################################################################
